package api

import (
	"Suscan/models"
	"Suscan/pkg/e"
	"Suscan/pkg/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetIplist(c *gin.Context) {
	url := c.Query("url")
	ip := c.Query("ip")
	port := c.Query("port")
	service := c.Query("service")
	res_code := c.Query("res_code")
	res_result := c.Query("res_result")
	res_url := c.Query("res_url")
	res_title := c.Query("res_title")
	create_time := c.Query("create_time")
	update_time := c.Query("update_time")
	maps := make(map[string]interface{})

	if url != "" {
		maps["url"] = url
	}
	if ip != "" {
		maps["ip"] = ip
	}
	if port != "" {
		maps["port"] = port
	}
	if service != "" {
		maps["service"] = service
	}
	if res_code != "" {
		maps["res_code"] = res_code
	}
	if res_result != "" {
		maps["res_result"] = res_result
	}
	if res_url != "" {
		maps["res_url"] = res_url
	}
	if res_title != "" {
		maps["res_title"] = res_title
	}
	if create_time != "" {
		maps["create_time"] = create_time
	}
	if update_time != "" {
		maps["update_time"] = update_time
	}

	code := e.SUCCESS

	pageSize, _  := strconv.Atoi(c.Query("pageSize"))

	data := models.GetIplist(utils.GetPage(c), pageSize, maps)
	total := models.GetIplistTotal(maps)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
		"total":total,
	})
}

type Iplist struct {
	ID []int `json:"id"`
}

func DeleteIplist(c *gin.Context) {
	json := Iplist{}
	c.BindJSON(&json)
	var code int
	data :=make(map[string]string)
	for _,v := range json.ID {
		a ,id := models.DeleteIplist(v)
		if a {
			code = e.SUCCESS
		}else {
			code = e.ERROR
			data["id"] = strconv.Itoa(id)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}